package com.example.attendance_back.service;

import com.example.attendance_back.mapper.StudentSignMapper;
import com.example.attendance_back.model.ResponseMessage;

import com.example.attendance_back.model.StudentSign;
import com.example.attendance_back.model.StudentSignWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;


import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service

public class StudentSignService {
    @Autowired
    private StudentSignMapper mapper;

    @Autowired
    private ResponseMessage responseMessage;


    public ResponseMessage signIn(Integer studentSignId, Date sign_time){

        ResponseMessage message = new ResponseMessage();

         StudentSign studentSign = new StudentSign();
        studentSign.setStudentSignId(studentSignId);
        studentSign.setSign_time(sign_time);

         Boolean success=   mapper.update(studentSign)>0?true:false;
         message.setSuccess(success);

         return  message;
    }






    @Cacheable(cacheNames = "byStudentId")
    public ResponseMessage getStuSignsByStudentId(Integer studentId){

         ResponseMessage message = new ResponseMessage();

        StudentSignWrapper wrapper = new StudentSignWrapper();
        wrapper.setStudentId(studentId);

        List<StudentSignWrapper>  list= mapper.selectByExample(wrapper);

        message.setData(list);
        message.setSuccess(true);

        return  message;


    }


    public ResponseMessage getStuSignsByTeacherId(Integer teacherId){


        ResponseMessage message = new ResponseMessage();

        StudentSignWrapper wrapper = new StudentSignWrapper();
        wrapper.setTeacherId(teacherId);

        List<StudentSignWrapper>  list= mapper.selectByExample(wrapper);

        message.setData(list);
        message.setSuccess(true);

        return  message;


    }


    public ResponseMessage save(StudentSign studentSign){
        /***
         * 如何判断 到底是添加还是修改
         *    主键id studentSignId 如果为空，就是添加操作
         *    否则就是修改操作
         * */
        Date current =new Date();
        studentSign.setSign_time(current);
        Boolean success=false;
        if(studentSign.getStudentSignId()!=-1){
           success=  mapper.update(studentSign)>0?true:false;
        }else {
            success=  mapper.insert(studentSign)>0?true:false;
        }
        responseMessage.setSuccess(success);
        return  responseMessage;
    }

}
